/*
 * LevelEditorWindow.cpp
 *
 *  Created on: 22/06/2014
 *      Author: javier
 */

#include "LevelEditorWindow.h"
#include "CampaignEditorWindow.h"
#include "Utils.h"
#include <iostream>
#include <vector>
#include "MetalWall.h"

LevelEditorWindow::LevelEditorWindow(BaseObjectType* cobject,
		const Glib::RefPtr<Gtk::Builder>& refGlade) :
		Gtk::Window(cobject) {

	refGlade->get_widget("btnBox", btnBox);
	refGlade->get_widget("btnMetal", btnMetal);
	refGlade->get_widget("btnPlant", btnPlant);
	refGlade->get_widget("btnSpawnZombie", btnSpawnZombie);
	refGlade->get_widget("btnSpawnBomberman", btnSpawnBomberman);
	refGlade->get_widget("btnBack", btnBack);

	refGlade->get_widget("gridLevel", gridLevel);

	refGlade->get_widget("inputIDLevel", inputID);
	refGlade->get_widget("inputEnemies", inputEnemies);

	btnSpawnZombie->signal_clicked().connect(
			sigc::mem_fun(*this, &LevelEditorWindow::addSpawnZombie));
	btnSpawnBomberman->signal_clicked().connect(
			sigc::mem_fun(*this, &LevelEditorWindow::addSpawnBomberman));
	btnBox->signal_clicked().connect(
			sigc::mem_fun(*this, &LevelEditorWindow::addBox));
	btnMetal->signal_clicked().connect(
			sigc::mem_fun(*this, &LevelEditorWindow::addMetal));
	btnPlant->signal_clicked().connect(
			sigc::mem_fun(*this, &LevelEditorWindow::addPlant));

	btnBack->signal_clicked().connect(
			sigc::mem_fun(*this, &LevelEditorWindow::back));

	campaignWindow = NULL;
	camp = NULL;
	level = NULL;
	btnSelected = NULL;
	idSequence = 0;
}

LevelEditorWindow::~LevelEditorWindow() {
	// TODO Auto-generated destructor stub
}

const CampaignEditorWindow* LevelEditorWindow::getCampaignWindow() const {
	return campaignWindow;
}

void LevelEditorWindow::setCampaignWindow(
		CampaignEditorWindow* campaignWindow) {
	this->campaignWindow = campaignWindow;
}

void LevelEditorWindow::back() {
	camp->addNivel(Utils::stringToInt(inputID->get_text()), *level);
//	controller->save();
	this->hide();
}

void LevelEditorWindow::setCampania(Campania* camp) {
	this->camp = camp;
}

void LevelEditorWindow::setLevel(Nivel* level) {
	this->level = level;
	std::string repr = level->getTileMapRepresentation();
	int width = repr.find('\n');
	int height = (repr.size() * (width - 1) / width) / width + 1;
	for (int y = 0; y < height; ++y) {
		for (int x = 0; x < width; ++x) {
			std::string tile;
			tile += (repr.at(y * (width + 1) + x));
			Gtk::Button *bTile = Gtk::manage(new Gtk::Button(tile.c_str()));
			TileButton* tb = new TileButton(bTile);
			tb->setX(x);
			tb->setY(y);
			tb->setText(tile.c_str());
			bTile->signal_clicked().connect(
					sigc::bind<TileButton*, int, int>(
							sigc::mem_fun(*this,
									&LevelEditorWindow::selectTile), tb, x, y));
			gridLevel->attach(*bTile, x, y, 1, 1);
			std::pair<int, int> key(x, y);
			buttons[key] = tb;
		}
	}
}

void LevelEditorWindow::setController(EditorController* controller) {
	this->controller = controller;
}

void LevelEditorWindow::selectTile(TileButton* btnSelected, int x, int y) {
	std::cout << btnSelected->getButton()->get_label() << " " << x << " " << y
			<< std::endl;
	this->btnSelected = btnSelected;
}

void LevelEditorWindow::addSpawnZombie() {
	btnSelected->getButton()->set_label("SZ");
}

void LevelEditorWindow::addSpawnBomberman() {
	btnSelected->getButton()->set_label("SB");
}

void LevelEditorWindow::addBox() {
	btnSelected->getButton()->set_label("BOX");
}

void LevelEditorWindow::addMetal() {
	btnSelected->getButton()->set_label("MET");
	MetalWall* m = new MetalWall(btnSelected->getX(), btnSelected->getY());
	m->setId(idSequence++);
	level->addMapObject(m);
}

void LevelEditorWindow::addPlant() {
	btnSelected->getButton()->set_label("PL");
}
